<?php
namespace app\admin\controller;
use think\Db;
class Scenicspot extends Base{
    public function spot_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['time', '>', $fromtime],
            ['time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['state','<',3]);
        }else{
            array_push($map,['state','=',$state]);
        }

        $list=Db::name('scenic_spot')->where($map)->where('title','like',"%{$keyword}%")->order('id desc')->paginate(10);
        $counts=Db::name('scenic_spot')->where($map)->where('title','like',"%{$keyword}%")->count();

        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('state',$state);
        $this->assign('list',$list);
        $this->assign('counts',$counts);
        return $this->fetch();
    }
    public function spot_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['instructions']=html_entity_decode($param['instructions']);
            $data['scenicspot_instructions']=html_entity_decode($param['scenicspot_instructions']);
            $data['tourism_strategy']=html_entity_decode($param['tourism_strategy']);
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            $data['imgs']=$param['imgs'];
            $data['price']=$param['price'];
            $data['address']=$param['address'];
            $data['location']=$param['location'];
            $data['latitude']=explode(',', $data['location'])[0];
            $data['longitude']=explode(',', $data['location'])[1];
            $data['state']=1;
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('scenic_spot')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function spot_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('scenic_spot')->where('id',$param['id'])->find();
            if($data['imgs']){
                $data['imgsarr']=explode(',',$data['imgs']);
            }
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['instructions']=html_entity_decode($param['instructions']);
            $data['scenicspot_instructions']=html_entity_decode($param['scenicspot_instructions']);
            $data['tourism_strategy']=html_entity_decode($param['tourism_strategy']);
            $data['detail']=html_entity_decode($param['detail']);
            $data['img']=$param['img'];
            if(substr($param['imgs'],0,1)==','){
                $data['imgs']=substr($param['imgs'],1);
            }else{
                $data['imgs']=$param['imgs'];
            }
            $data['price']=$param['price'];
            $data['address']=$param['address'];
            $data['location']=$param['location'];
            $data['latitude']=explode(',', $data['location'])[0];
            $data['longitude']=explode(',', $data['location'])[1];
            $data['state']=1;
            $data['uptime']=time();
            $res= Db::name('scenic_spot')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('scenic_spot')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('scenic_spot')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function spot_del(){
        $param = input('post.');
        $res= Db::name('scenic_spot')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
